﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace NDBGen
{
    public class ReadOnlyDictionary<TKey, TValue> : IEnumerable, IEnumerable<KeyValuePair<TKey, TValue>>
    {
        private IDictionary<TKey, TValue> innerDictionary;

        public ReadOnlyDictionary(IDictionary<TKey, TValue> dictionary)
        {
            innerDictionary = dictionary;
        }

        public Boolean ContainsKey(TKey key)
        {
            return innerDictionary.ContainsKey(key);
        }

        public Boolean TryGetValue(TKey key, out TValue value)
        {
            return innerDictionary.TryGetValue(key, out value);
        }

        public TValue this[TKey key]
        {
            get { return innerDictionary[key]; }
        }

        public Int32 Count
        {
            get { return innerDictionary.Count; }
        }

        #region [ IEnumerable<KeyValuePair<TKey,TValue>> Members ]

        IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
        {
            return innerDictionary.GetEnumerator();
        }

        #endregion

        #region [ IEnumerable Members ]

        IEnumerator IEnumerable.GetEnumerator()
        {
            return innerDictionary.GetEnumerator();
        }

        #endregion

        public override Boolean Equals(Object obj)
        {
            var that = obj as ReadOnlyDictionary<TKey, TValue>;
            if (that == null) return false;
            else return
                this.innerDictionary.Keys.SequenceEqual(that.innerDictionary.Keys) &&
                this.innerDictionary.Values.SequenceEqual(that.innerDictionary.Values);
        }

        public ReadOnlyDictionary<TKey, TValue2> ToParentValueDictionary<TValue2>()
        {
            return new ReadOnlyDictionary<TKey, TValue2>(this.ToDictionary(e => e.Key, e => (TValue2)(Object)e.Value));
        }
    }
}